﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FillCartridgeBBB.Models
{
    public class CustomerRepository
    {
        private FillCartridgeDbEntities _db;

        public CustomerRepository(FillCartridgeDbEntities db)
        {
            _db = db;
        }

        public IEnumerable<Customer> SelectAll ()
        {
            return _db.CustomerSet;
        }

        public Customer SelectById (int id)
        {
            return _db.CustomerSet.First(c => c.CustomerId == id);
        }

        public void Insert (Customer customer)
        {
            _db.AddToCustomerSet(customer);
            _db.SaveChanges();
            return;
        }

        public void Update (Customer customer)
        {
            Customer dbCust = SelectById(customer.CustomerId);
            dbCust.Adress = customer.Adress;
            dbCust.Name = customer.Name;
            dbCust.Phone = customer.Phone;
            _db.SaveChanges();
        }

        public void Delete (int id)
        {
            _db.DeleteObject(SelectById(id));
            _db.SaveChanges();
        }
    }
}
